/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.chooser.view;

import offset.nodes.client.chooser.view.NodeChooserDialog;
import java.net.URL;
import java.util.ResourceBundle;
import offset.nodes.Constants;
import offset.nodes.client.chooser.model.GetNodeByPath;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.dialog.chooser.model.CopyNode;
import offset.nodes.client.view.BaseApplet;

/**
 *
 * @author Walter Lütgenau
 */
public class CopyNodeApplet extends BaseApplet {
    NodeChooserDialog dialog = null;
    ServerModel model = null;
    String fromPath = null;
    public static final String PAR_PATH = "path";
    public static final String PAR_SERVICE = "service";
    public static final String PAR_REPOSITORY = "repository";
    public static final String PAR_ROOT = "root";
    public static final String PAR_FILTER = "filter";

    
    /** Creates a new instance of NodeChooserApplet */
    public CopyNodeApplet() {
    }
    
    protected String getLastPathComponent(String path) {
        int index = path.lastIndexOf("/");
        if ( index < 0)
            return path;
        return path.substring(index+1);
    }
    
    protected boolean nodeExists(String path) throws Exception {
        GetNodeByPath.Request request = new GetNodeByPath.Request();
        request.setPath(path);
        GetNodeByPath.Response response = (GetNodeByPath.Response) model.sendRequest(request);
        
        return response.getNode() != null;
    }
    
    public void startUI() {
        dialog.setVisible(true);
        int result = dialog.getReturnStatus();
        try {
            URL serverUrl = new URL(getParameter(PAR_SERVICE));
            URL url = null;
            if ( result == NodeChooserDialog.RET_OK) {
                CopyNode.Request request = new CopyNode.Request();
                request.setFromPath(fromPath);
                request.setToPath(dialog.getPath());
                
                CopyNode.Response response = (CopyNode.Response) model.sendRequest(request);

                if (dialog.useNewTab()) {
                    getAppletContext().showDocument(new URL(getParameter(PAR_REPOSITORY) + Constants.PATH_SEPARATOR + response.getResultNodePath()), BaseApplet.SHOW_NEW_TAB);
                    return;
                } else
                    url = new URL(getParameter(PAR_REPOSITORY) + Constants.PATH_SEPARATOR + response.getResultNodePath());
            }
            else 
                url = new URL(getParameter(PAR_REPOSITORY) + Constants.PATH_SEPARATOR + fromPath );
        
             getAppletContext().showDocument(url);
        } catch( Exception e) {
            e.printStackTrace();
        }
    }
    
    public void initUI() {
        try {
            fromPath = getParameter(PAR_PATH);
            String url = getParameter(PAR_SERVICE);
            String root = getParameter(PAR_ROOT);
            String filter = getParameter(PAR_FILTER);
            
            model = new ServerModel(new URL(url));
            dialog = new NodeChooserDialog(new javax.swing.JFrame(), true, model, root, filter);
            dialog.setLocation(100,100);
            dialog.setTitle(ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle").getString("copy.title"));
        } catch( Exception e) {
            e.printStackTrace();
        }
    }
    
}
